package com.whut.cristiano.autographbook.Model;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.whut.cristiano.autographbook.MyApplication;
import com.whut.cristiano.autographbook.R;
import com.whut.cristiano.autographbook.Util.ImageUtil;
import com.whut.cristiano.autographbook.Util.LogUtil;
import com.whut.cristiano.autographbook.Util.NetUtil;
import com.whut.cristiano.autographbook.Util.SharedPrefUtil;
import com.whut.cristiano.autographbook.Util.ToastUtil;

/**
 * Created by ykbboom on 2017/4/12 17:50
 * email：ykbpro@whut.edu.cn
 */

public class PhoneDialog extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.phone_dialog, null);
        final EditText editText = (EditText) view.findViewById(R.id.receive_phone);
        ImageView card = (ImageView) view.findViewById(R.id.card_preview);
        Glide.with(getContext()).load(SharedPrefUtil.getTemplateOnePicUrl()).into(card);
        final Activity activity = getActivity();
        LogUtil.d("PhoneDialog",activity.getPackageName());
        final ProgressDialog progressDialog = new ProgressDialog(activity);
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(view)
                // Add action buttons
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                //上传到OSS服务器,服务器保存数据
                                if (SharedPrefUtil.getTemplateOnePicUrl()==null){
                                    ToastUtil.showToast(activity,"请先保存贺卡");
                                }else {
                                    ImageUtil.TemplateUploadPhoto(progressDialog, new HandlePictureListener() {
                                        @Override
                                        public void onStart() {}

                                        @Override
                                        public void onStop(String picUrl, Boolean isUploaded, String time) {
                                            progressDialog.dismiss();
                                            if (isUploaded){
                                                showDialog(progressDialog);
                                                NetUtil.sendCard(SharedPrefUtil.getUserPhone(),editText.getText().toString(),picUrl,time,progressDialog);
                                            }else{
                                                ToastUtil.showToast(activity,"未知异常发生。");
                                            }

                                        }

                                        @Override
                                        public void onError(Exception e) {}
                                    },SharedPrefUtil.getTemplateOnePicUrl());
                                }
                            }
                        }).setNegativeButton("取消", null);
        return builder.create();
    }
    public void showDialog(ProgressDialog progressDialog) {
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("正在发送贺卡...");
        progressDialog.show();
    }
}
